package cn.fzm.ssm.dao;

import cn.fzm.ssm.domain.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Dremeue
 * @create 2019-10-12 12:50
 **/
public interface IUserDao {

    @Select("select * from users where username = #{username}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "email", property = "email"),
            @Result(column = "password", property = "password"),
            @Result(column = "phoneNum", property = "phoneNum"),
            @Result(column = "status", property = "status"),
            @Result(column = "id", property = "roles", many = @Many(select = "cn.fzm.ssm.dao.IRoleDao.findByUserId")),
    })
    public UserInfo findByUsername(String username);

    @Select("select * from users")
    public List<UserInfo> findAll();

    @Insert("insert into users(id, email, username, password, phoneNum, status) values(#{id}, #{email}, #{username}," +
            "#{password}, #{phoneNum}, #{status} )")
    public void save(UserInfo userInfo);

    @Select("select * from users where id = #{id}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "email", property = "email"),
            @Result(column = "password", property = "password"),
            @Result(column = "phoneNum", property = "phoneNum"),
            @Result(column = "status", property = "status"),
            @Result(column = "id", property = "roles", many = @Many(select = "cn.fzm.ssm.dao.IRoleDao.findByUserId")),
    })
    public UserInfo findById(String id);

    @Delete("<script>delete from users where id in <foreach collection='array' item='id' open='(' separator=',' " +
            "close=')'>#{id}</foreach></script>")
    public void deleteByIds(String[] ids);

    @Insert("insert into users_role(userId, roleId) values(#{userId}, #{roleId})")
    public void addRoleToUser(@Param("userId") String userId, @Param("roleId") String roleId);
}
